package com.zretc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.zretc.dao.AttentionDao;
import com.zretc.entity.Attention;
import com.zretc.entity.Singer;
import com.zretc.util.DBUtil;
import com.zretc.util.PageInfo;

/**
 * 关注列表dao实现类
 * @author Administrator
 *
 */

public class AttentionDaoImpl implements AttentionDao {

	@Override
	public int insertAttention(Integer accountId, Integer singerId) {
		int n = DBUtil.doUpdate("insert into attention values(?,?)", accountId,singerId);
		return n;
	}

	@Override
	public Attention findAttentionByAccountIdAndSingerId(Integer accountId, Integer singerId) {
		Attention attention = null;
		ResultSet rs = DBUtil.doQuery("select account_id,singer_id from attention where account_id = ? and singer_id = ?", accountId,singerId);
			
		try {
			while (rs.next()) {
				attention = new Attention(accountId, singerId);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return attention;
	}

	@Override
	public int deleteAttentionByAccountIdAndSingerId(Integer accountId, Integer singerId) {
		int n = DBUtil.doUpdate("delete from attention where account_id = ? and singer_id = ?", accountId,singerId);
		return n;
	}

	@Override
	public int attentionCount(Integer accountId) {
		int n = 0;
		ResultSet rs = DBUtil.doQuery("select count(*),account_id,singer_id from attention where account_id = ?", accountId);
		
		try {
			while (rs.next()) {
				n = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public PageInfo<Attention> findAttentionByAccountId(Map<String, String> map) {
		PageInfo<Attention> pageInfo = null;
		Attention attention = null;
		List<Attention> list = new ArrayList<>();
		Singer singer = null;
		Integer accountId = Integer.parseInt(map.get("accountId"));
		Integer pageNum = Integer.parseInt(map.get("pageNum"));
		Integer pageSize = Integer.parseInt(map.get("pageSize"));
		ResultSet rs = DBUtil.doQuery("select A.account_id,B.singer_id,B.singer_name,B.gender,B.age,B.singer_picture,B.area_id from"
				+ " attention A left join singer B on A.singer_id = B.singer_id where A.account_id = ? limit ?,?", accountId,(pageNum-1)*pageSize,pageSize);
		
		try {
			while (rs.next()) {
				Integer singerId = rs.getInt("singer_id");
				String singerName = rs.getString("singer_name");
				String gender = rs.getString("gender");
				Integer age = rs.getInt("age");
				String singerPicture = rs.getString("singer_picture");
				singer = new Singer(singerId, singerName, gender, age, singerPicture, null);
				attention = new Attention(accountId, singer);
				list.add(attention);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		Integer total = 0;
		rs = DBUtil.doQuery("select count(*),A.account_id,B.singer_id,B.singer_name,B.gender,B.age,B.singer_picture,B.area_id from"
				+ " attention A left join singer B on A.singer_id = B.singer_id where A.account_id = ?", accountId);
		
		try {
			while (rs.next()) {
				total = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		pageInfo = new PageInfo<>(list, pageNum, pageSize, total);
		return pageInfo;
	}
	
}
